package tq.com.tqteacher.adapt;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.vh.CommonViewHolder;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQAtachModel;
import tq.com.tqteacher.view.viewhold.TQLeaveMsgViewHold;

public class TQJPLeaveMsgAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private ArrayList<TQAtachModel> mAtachModels = new ArrayList<>();
    public TQJPLeaveMsgAdapter(Context pContext){
        mContext = pContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, final int pI) {
        return CommonViewHolder.sCommonViewHolder(mContext, R.layout.vh_pj_leavemsg_layout, pViewGroup)
                .setHolderBinder(new CommonViewHolder.IViewHolder() {
                    @Override
                    public void bind(View pView, Object data) {
                        if(data == null || !(data instanceof TQAtachModel)) return;

                        final TQAtachModel lAtachModel = (TQAtachModel) data;
                        ImageView picture = pView.findViewById(R.id.picture);
                        View lView = pView.findViewById(R.id.levaeMsg);
                        if(lAtachModel.getComment() == null){
                            lView.setVisibility(View.GONE);
                        }else {
                            lView.setVisibility(View.VISIBLE);
                            TQLeaveMsgViewHold lTQLeaveMsgViewHold = new TQLeaveMsgViewHold(lView);
                            lTQLeaveMsgViewHold.setComment(lAtachModel.getComment());
                        }

                        String url = TQPictureUpload.downloadPath(lAtachModel.getCover());
                        RequestOptions lOptions = new RequestOptions().placeholder(R.drawable.ic_default_picture)
                                .error(R.drawable.ic_default_picture);
                        Glide.with(mContext).load(url).apply(lOptions).into(picture);

                        picture.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if(mIClickListener != null)mIClickListener.openCover(lAtachModel);
                            }
                        });
                        lView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if(mIClickListener != null)mIClickListener.openComment(lAtachModel);
                            }
                        });
                    }
                });
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        if(pViewHolder instanceof CommonViewHolder){
            (((CommonViewHolder) pViewHolder)).bindData(mAtachModels.get(pI));
        }
    }

    @Override
    public int getItemCount() {
        return mAtachModels.size();
    }

    public void clear(){
        mAtachModels.clear();
        notifyDataSetChanged();
    }
    public void append(ArrayList<TQAtachModel> pTQAtachModels){
        mAtachModels.addAll(pTQAtachModels);
        notifyDataSetChanged();
    }

    public void setIClickListener(IClickListener pIClickListener){
        mIClickListener = pIClickListener;
    }
    private IClickListener mIClickListener;
    public interface IClickListener{
        void openCover(TQAtachModel pModel);
        void openComment(TQAtachModel pModel);
    }
}
